package org.jaaksi.demo.layoutinflate;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.LinearLayout;

/**
 * Created by Fcy on 2016/7/2.
 */
public class TestMergeActivity extends Activity {

    private ViewStub viewStub;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merge);

        LinearLayout container = (LinearLayout) findViewById(R.id.container);

        // 如果infalte 根布局是<merge/>标签的xml布局，必须指明parentView并且attach，
        // 也就是说<merge/>不是个view，不能独立存在，只是为了减少层级，必须添加到父容器中
        // fixme <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
//        View inflate = LayoutInflater.from(this).inflate(R.layout.lib_test_merge, null);

//        View.inflate(this, R.layout.lib_test_merge, container);

         /*fixme ViewStub不能与<merge/>标签混用，<include/>是可以的，这与ViewStub有关
           因为ViewStub一旦调用了setVisible(Visible)或者inflate()之后，就会消失，替换为layout所指向的布局view
            而根布局标签时<merge/>是无法提出单独的view的 */
        viewStub = (ViewStub) findViewById(R.id.view_stub);
        // ViewStub一旦调用了setVisible(Visible)或者inflate()之后，就会消失，替换为layout所指向的根布局view
        // 然后又必须通过infalte才能拿到layout指向的view。才能操作layout中的view，所以很尴尬
//        viewStub.inflate();
    }

    boolean flag = true;

    public void onClick(View view) {
        if (flag) {
            viewStub.setVisibility(View.VISIBLE);
        } else {
            viewStub.setVisibility(View.GONE);
        }
        flag = !flag;
    }
}